<!DOCTYPE HTML>
<html  lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-workInfo-edit" th:object="${workInfo}">
            <input id="workId" name="workId" th:field="*{workId}"  type="hidden">
            <div class="form-group">	
                <label class="col-sm-3 control-label">证件号码：</label>
                <div class="col-sm-8">
                    <input id="empId" name="empId" th:field="*{empId}" class="form-control" type="text" readonly="readonly">
                </div>
            </div>
            <div class="form-group">	
                <label class="col-sm-3 control-label">姓名：</label>
                <div class="col-sm-8">
                    <input id="empNm" name="empNm" th:field="*{empNm}" class="form-control" type="text" readonly="readonly">
                </div>
            </div>
            <div class="form-group">	
                <label class="col-sm-3 control-label">所属公司名称：</label>
                <div class="col-sm-8">
                    <input id="empComp" name="empComp" th:field="*{empComp}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">	
                <label class="col-sm-3 control-label">职位：</label>
                <div class="col-sm-8">
                    <input id="empJob" name="empJob" th:field="*{empJob}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
				<label class="col-sm-3 control-label">日期： </label>
				<div class="col-sm-8 form-inline select-month">
						<input type="text" class="form-control time-input" id="startTime" placeholder="开始时间" name="params[beginTime]" th:value="${#dates.format(workInfo.empWorkStdt, 'yyyy-MM')}" />
						<span class="form-control text-center" style="border:0;">至</span>
						<input type="text" class="form-control time-input" id="endTime" placeholder="结束时间" name="params[endTime]" th:value="${#dates.format(workInfo.empWorkEddt, 'yyyy-MM')}"/>
				</div>
			</div>
		</form>
    </div>
    <div th:include="include::footer"></div>
    
    <script type="text/javascript">
		var prefix = ctx + "pm/workInfo";
		// laydate 时间控件绑定
		layui.use('laydate', function() {
		    var laydate = layui.laydate;
		    var startDate = laydate.render({
		        elem: '#startTime',
		        max: $('#endTime').val(),
		        theme: 'default',
		        type: 'month',
		        trigger: 'click',
		        done: function(value, date) {
		            // 结束时间大于开始时间
		            if (value !== '') {
		                endDate.config.min.year = date.year;
		                endDate.config.min.month = date.month - 1;
		                endDate.config.min.date = date.date;
		            } else {
		                endDate.config.min.year = '';
		                endDate.config.min.month = '';
		                endDate.config.min.date = '';
		            }
		        }
		    });
		    var endDate = laydate.render({
		        elem: '#endTime',
		        // min: $('#startTime').val(),
		        theme: 'default',
		        type: 'month',
		        trigger: 'click',
		        done: function(value, date) {
		            // 开始时间小于结束时间
		            if (value !== '') {
		                startDate.config.max.year = date.year;
		                startDate.config.max.month = date.month - 1;
		                startDate.config.max.date = date.date;
		            } else {
		                startDate.config.max.year = '';
		                startDate.config.max.month = '';
		                startDate.config.max.date = '';
		            }
		        }
		    });
		});
	
		
		$("#form-workInfo-edit").validate({
			rules:{
                empComp:{
                    required:true
                },
                empWorkStdt:{
                    required: true
                },
                empWorkEddt: {
                    required: true
                },
                empJob: {
                    required: true
                }
			}
		});

        function submitHandler() {
            if ($.validate.form()) {
                var result = checkDateOverlap();
                if (result.toString() === "1") {
                    edit() ;
                } else {
                    $.modal.alertError("时间重叠");
                }
            }
        }

        function checkDateOverlap() {
            var flag = "";
            var empPjStdt = $("#startTime").val();
            var empPjEddt = $("#endTime").val();
            var empId = $("input[name='empId']").val();
            var workId = $("input[name='workId']").val();
            $.ajax({
                cache : true,
                type : "POST",
                url : ctx + "pm/workInfo/checkDate",
                data : {
                    "empId": empId,
                    "startDate": empPjStdt,
                    "endDate": empPjEddt,
                    "operationType": "edit",
                    "id": workId
                },
                async : false,
                error : function(request) {
                    $.modal.alertError("时间重叠");
                },
                success : function(data) {
                    flag = data;
                }
            });
            return flag;
        }
		
		function edit() {
			var workId = $("input[name='workId']").val();
			var empId = $("input[name='empId']").val();
        	var empNm = $("input[name='empNm']").val();
        	var empComp = $("input[name='empComp']").val();
        	var empJob = $("input[name='empJob']").val();
        	var empWorkStdt = $("#startTime").val();
        	var empWorkEddt = $("#endTime").val();
        	$.ajax({
        		cache : true,
        		type : "POST",
        		url : ctx + "pm/workInfo/edit",
        		data : {
        			"workId": workId,
        			"empId": empId,
        			"empNm": empNm,
        			"empComp": empComp,
        			"empWorkStdt": empWorkStdt,
        			"empWorkEddt": empWorkEddt,
        			"empJob": empJob
        		},
        		async : true,
        		error : function(request) {
        			$.modal.alertError("系统错误");
        		},
        		success : function(data) {
        			$.operate.saveSuccess(data);
        		}
        	});
        }
	</script>
</body>
</html>
